package com.atguigu.java2;

import javax.management.relation.RoleUnresolved;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 创建线程的方式四：使用线程池
 */

class NumThread1 implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                System.out.println(i);
            }
        }
    }
}

public class ThreadPool {
    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(10);

        ThreadPoolExecutor service1 = (ThreadPoolExecutor) service;

        service1.setKeepAliveTime(100, TimeUnit.SECONDS);
        service1.setCorePoolSize(5);

        service.execute(new NumThread1());//适合使用runnable
//        service.submit();//适合使用callable

        service.shutdown();

    }
}
